package net.tngou.iask.action.api;

import java.util.HashMap;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.enums.LevelEnum;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Question;
import net.tngou.iask.pojo.Question_answer;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.pojo.Survey_disease;
import net.tngou.iask.pojo.Survey_question;
import net.tngou.iask.service.QuestionService;
import net.tngou.iask.service.SurveyService;
import net.tngou.iask.util.StringUtil;

/**
 * 问卷调查
 * @author tngou@tngou.net
 *
 */
public class SurveyApi extends BaseAction {

	/**
	 * 添加问卷
	 */
	public void add() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有添加权限");return;
		}
		Survey bean = getAsk(Survey.class);
		if(isEmpty(bean.getName()))
		{
			run_false("请求的参数不正确！");return;
		}
		POJO pojo = bean.get(new HashMap<String, Object>(){{
			put("name", bean.getName());
			put("main", user.getMain());
		}});
		if(pojo!=null)
		{
			run_false("已经存在该标签！");return;
		}
		bean.setMain(user.getMain());
		long id = bean.save();
		
		if(id>0){
			
			saveLog(LevelEnum.system, "添加问卷(id:"+id+"-"+bean.getName()+")");
			run_success(id+"");
		}
		else {
			run_false("添加失败！");
		}
	}
	/**
	 * 更新问卷
	 */
	public void update() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有修改权限");return;
		}
		Survey bean = getAsk(Survey.class);
		if(bean.getId()==0){
			run_false("请求的参数不正确！");return;
		}
		Survey survey = new Survey().get(bean.getId());		
		if(survey==null)
		{
			run_false("请求的参数不正确！");return;
		}
		if(StringUtil.isNotEmpty(bean.getName())&&!bean.getName().equals(survey.getName()))
		{
			POJO pojo = bean.get(new HashMap<String, Object>(){{
				put("name", bean.getName());
				put("main", user.getMain());
			}});
			if(pojo!=null)
			{
				run_false("已经存在该名称！");return;
			}
			survey.setName(bean.getName());
		}
	
		if(StringUtil.isNotEmpty(bean.getDescription()))survey.setDescription(bean.getDescription());
		if(StringUtil.isNotEmpty(request.getParameter("type")))survey.setType(bean.getType());
		long id = survey.update();
		if(id>0){
			saveLog(LevelEnum.system, "修改问卷(id:"+id+"-"+bean.getName()+")");
			run_success("修改成功");
		}
		else {
			run_false("修改失败！");
		}
	}
	
	/**
	 * 更新问题顺序
	 */
	public void seq() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有修改权限");return;
		}
		Survey_question bean = getAsk(Survey_question.class);
		if(bean.getId()==0||bean.getSeq()==0){
			run_false("请求的参数不正确！");return;
		}
		Survey_question survey_question=new Survey_question().get(bean.getId());
		if(survey_question==null){
			run_false("请求的参数不正确！");return;
		}
		survey_question.setSeq(bean.getSeq());
		long id = survey_question.update();
		if(id>0){
			saveLog(LevelEnum.system, "修改问题顺序(id:"+id+")");
			run_success("修改成功");
		}
		else {
			run_false("修改失败！");
		}
	}
	
	/**
	 * 添加问题
	 */
	public void qusetionadd() {
		
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有修改权限");return;
		}
		Survey_question bean = getAsk(Survey_question.class);
		if(bean.getQuestion()==0||bean.getSurvey()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Survey_question survey_question = new Survey_question().get("survey="+bean.getSurvey()+" AND question="+bean.getQuestion());
		if(survey_question!=null){
			run_success("添加成功");return;
		}
		Survey survey = new Survey ().get(bean.getSurvey ());
		Question question = new Question().get(bean.getQuestion());
		if(survey==null||question==null||survey.getMain()!=user.getMain())
		{
			run_false("请求的参数不正确！");return;
		}
		int seq = bean.totalCount("survey="+bean.getSurvey())+1;
		bean.setSeq(seq);
		long id = bean.save();
		if(id>0){
			saveLog(LevelEnum.system, "添加问题(id:"+id+")");
			survey.setQuestioncount(survey.getQuestioncount()+1);
			survey.update();
			run_success("添加成功");
		}
		else {
			run_false("添加失败！");
		}
	}
	
	
	/**
	 * 删除问题
	 */
	public void questiondelete() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有删除权限");return;
		}
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Survey_question bean =new Survey_question().get(ask.getId());
		if(bean!=null)
		{
			Survey survey = new Survey().get(bean.getSurvey());
			if(survey==null||survey.getMain()!=user.getMain())
			{
				run_false("没有删除权限");return;
			}else
			{
				boolean b = bean.delete();
				if(b){
					saveLog(LevelEnum.system, "删除问题(id:"+survey.getId()+")");
					survey.setQuestioncount(survey.getQuestioncount()-1);
					survey.update();
					run_success("删除成功");
				}else
				{
					run_false("删除失败！");return;
				}
			}
		}else
		{
			run_false("请求的参数不正确！");return;
		}
	
	}
	/**
	 * 添加问题
	 */
	public void diseaseadd() {
		
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有修改权限");return;
		}
		Survey_disease bean = getAsk(Survey_disease.class);
		if(bean.getDisease()==0||bean.getSurvey()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Survey_disease survey_disease = new Survey_disease().get("survey="+bean.getSurvey()+" AND disease="+bean.getDisease());
		if(survey_disease!=null){
			run_success("添加成功");return;
		}
		Survey survey = new Survey ().get(bean.getSurvey ());
		Disease disease = new Disease().get(bean.getDisease());
		if(survey==null||disease==null||survey.getMain()!=user.getMain())
		{
			run_false("请求的参数不正确！");return;
		}
		long id = bean.save();
		if(id>0){
			saveLog(LevelEnum.system, "添加疾病(id:"+id+")");
			survey.setDiseasecount(survey.getDiseasecount()+1);
			survey.update();
			run_success("添加成功");
		}
		else {
			run_false("添加失败！");
		}
	}
	
	
	/**
	 * 删除问题
	 */
	public void diseasedelete() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有删除权限");return;
		}
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Survey_disease bean =new Survey_disease().get(ask.getId());
		if(bean!=null)
		{
			Survey survey = new Survey().get(bean.getSurvey());
			if(survey==null||survey.getMain()!=user.getMain())
			{
				run_false("没有删除权限");return;
			}else
			{
				boolean b = bean.delete();
				if(b){
					saveLog(LevelEnum.system, "删除问题(id:"+survey.getId()+")");
					survey.setDiseasecount(survey.getDiseasecount()-1);
					survey.update();
					run_success("删除成功");
				}else
				{
					run_false("删除失败！");return;
				}
			}
		}else
		{
			run_false("请求的参数不正确！");return;
		}
	}
	
}
